//package com.resou.reader.bookstore.category;
//
//import android.annotation.SuppressLint;
//import android.content.Context;
//import android.support.annotation.NonNull;
//import android.support.v4.widget.SwipeRefreshLayout;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//
//import com.resou.reader.R;
//import com.resou.reader.api.ApiImp;
//import com.resou.reader.api.entry.BookCategoryBean;
//import com.resou.reader.api.entry.BookCategoryData;
//import com.resou.reader.api.entry.Result;
//import com.resou.reader.api.entry.SortSecBean;
//import com.resou.reader.api.service.StoreService;
//import com.resou.reader.base.v.BaseFragment;
//import com.resou.reader.bookstore.category.adapter.SubCategoryAdapter;
//import com.resou.reader.utils.log.DLog;
//import com.resou.reader.view.DividerItemDecoration;
//
//import java.util.List;
//
//import butterknife.BindView;
//import io.reactivex.Observer;
//import io.reactivex.android.schedulers.AndroidSchedulers;
//import io.reactivex.disposables.Disposable;
//import io.reactivex.schedulers.Schedulers;
//
///**
// * Created by ${Garrett} on 2018/9/29.
// * Contact me krouky@outlook.com
// */
//@SuppressLint("ValidFragment")
//public class SubCategoryFragment extends BaseFragment {
//
//    private Disposable mDisposable;
//
//    private int parentId;
//    private SortSecBean sortSecBean;
//
//    @BindView(R.id.recyclerView)
//    RecyclerView mRecyclerView;
//    @BindView(R.id.swipe_refresh_layout)
//    SwipeRefreshLayout mSwipeRefreshLayout;
//    private DividerItemDecoration itemDecoration;
//
//    private int page = 1;
//    private int pageTotal;
//    private SubCategoryAdapter mAdapter = new SubCategoryAdapter();
//
//    public SubCategoryFragment(){}
//
//    public SubCategoryFragment(int parentId, SortSecBean sortSecBean) {
//        this();
//        this.parentId = parentId;
//        this.sortSecBean = sortSecBean;
//    }
//
//    @Override
//    protected int getLayoutId() {
//        return R.layout.fragment_sub_layout;
//    }
//
//    @Override
//    protected void initViews() {
//        mSwipeRefreshLayout.setEnabled(false);
//        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.btn_login_highlight));
//        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                mSwipeRefreshLayout.setRefreshing(false);
//            }
//        });
//        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
//        mRecyclerView.setLayoutManager(layoutManager);
//        mRecyclerView.setAdapter(mAdapter);
//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    if (layoutManager.findLastVisibleItemPosition() == mAdapter.getItemCount() - 1) {
//                        requestData();
//                    }
//                }
//            }
//        });
//        requestData();
//    }
//
//    @Override
//    protected void initPresenter() {
//    }
//
//    @Override
//    protected void initData() {
//
//    }
//
//    @Override
//    public Context getViewContext() {
//        return getContext();
//    }
//
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        if (mDisposable != null)
//            mDisposable.dispose();
//    }
//
//    public int getParentId() {
//        return parentId;
//    }
//
//    public SortSecBean getSortSecBean() {
//        return sortSecBean;
//    }
//
//    private void requestData() {
//        if (page > pageTotal && page != 1) {
//            mAdapter.updateList(null, false);
//            if(mSwipeRefreshLayout != null) {
//                mSwipeRefreshLayout.setRefreshing(false);
//            }
//            return;
//        }
//        mSwipeRefreshLayout.setEnabled(false);
//        if(mSwipeRefreshLayout != null) {
//            mSwipeRefreshLayout.setRefreshing(true);
//        }
//        int secCategoryId = sortSecBean.getSecCategoryId();
//        DLog.d(SubCategoryFragment.class.getSimpleName(), "secCategoryId = " + secCategoryId);
//        ApiImp.getInstance().getService(StoreService.class).searchbycategory(secCategoryId, 0, 0, page, 30)
//                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<Result<BookCategoryData>>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        mDisposable = d;
//                    }
//
//                    @Override
//                    public void onNext(Result<BookCategoryData> result) {
//                        DLog.i(SubCategoryFragment.class.getSimpleName(), "requestData searchbycategory success! code == " + result.getCode());
//                        // ...
//                        if(mSwipeRefreshLayout != null) {
//                            mSwipeRefreshLayout.setRefreshing(false);
//                        }
//                        BookCategoryData bookCategoryData = result.getData();
//                        pageTotal = bookCategoryData.getMaxPage();
//                        List<BookCategoryBean> list = bookCategoryData.getBookList();
//                        boolean hasMore = pageTotal != 0 && !(page == pageTotal - 1) && (list != null && list.size() > 0);
//                        if (itemDecoration == null && hasMore) {
//                            itemDecoration = new DividerItemDecoration(getActivity());
//                            mRecyclerView.addItemDecoration(itemDecoration);
//                        }
//                        page++;
//                        mAdapter.updateList(list, hasMore);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        DLog.i(SubCategoryFragment.class.getSimpleName(), "requestData searchbycategory error ! msg :" + e.getMessage());
//                        e.printStackTrace();
//                        if(mSwipeRefreshLayout != null) {
//                            mSwipeRefreshLayout.setRefreshing(false);
//                        }
//                        mAdapter.updateList(null, false);
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        if(mSwipeRefreshLayout != null) {
//                            mSwipeRefreshLayout.setRefreshing(false);
//                        }
//                    }
//                });
//    }
//}
